import http from '@/tool/http'
import axios from 'axios'
import url from '@/tool/api'
const state = {
	count:0,
	roleData:[]
}
const mutations = {
	setCount(state,num){
		state.count = num 
	},
	setData(state,arr){
		state.roleData = []
		state.roleData = arr
	},
	delData(state,id){
		// state.roleData.
	}
}
const actions = {
	getqxData({commit},num){
		if(num === undefined){
			num = 0
		}else{
			num = (num-1)*5
		}
		return new Promise((resolve,reject)=>{
			http({
				url:url.getRoleCount,
				method:'post',
			}).then(res=>{
				commit('setCount',res.data.count)
				resolve(res.data.count)
			})
		}).then(res=>{
			http({
				url:url.getRole,
				method:'post',
				data:{
					num1:num,
					num2:5
				}
			}).then(res=>{
				// console.log(res.data.data)
				if(res.data.code==='200'){
					commit('setData',res.data.data)
				}
				return res.data.code
			})
		})
	},
	editqx({commit},obj){
		console.log(obj)
		return http({
			url:url.upateRole,
			method:'post',
			data:obj
		}).then(res=>{
			console.log(res)
			if(res.data.code === '200'){
				// actions.getqxData({commit})
			}
			return res.data.code
		})
	},
	addqx({commit},obj){
		return http({
			url:url.addRole,
			method:'post',
			data:obj
		}).then(res=>{
			console.log(res)
			return res.data.code
		})
	},
	delqx({commit},id){
		return http({
			url:url.delRole,
			method:'post',
			data:{id:id}
		}).then(res=>{
			console.log(res)
			if(res.data.code === '200'){
				// commit('delData',obj.index)
			}
			return res.data.code
		})
	}
}
const getters = {
	tableqxData(state){
		let arr = state.roleData
		arr.forEach(item=>{
			if(item.create_by === 0){
				item.create_by = '系统默认'
			}
			//number不能转数组 只能先转成字符串在转数组 toString()
			if(item.permission_id){
				item.permission_id = item.permission_id.toString()
				// let str = ''
				let arr = []
				for(let i=0;i<item.permission_id.length;i++){
				switch(item.permission_id[i]){
					case '1':
					// str += '菜单管理/'
					arr.push('菜单管理/')
					break;
					case '2':
					// str += '运营管理/'
					arr.push('运营管理/')
					break;
					case '3':
					// str += '库存管理/'
					arr.push('库存管理/')
					break;
					case '4':
					// str += '信息管理/'
					arr.push('信息管理/')
					break;
					case '5':
					// str += '财务管理/'
					arr.push('财务分析/')
					break;
					case '6':
					// str += '系统设置/'
					arr.push('系统设置/')
					break;
					case '7':
					// str += '前台系统/'
					arr.push('前台系统/')
					break;
				}
				}
				item.permission_id = arr
			}
			
		})
		return arr
	}
}

export default {
	state,mutations,actions,getters
}